/**
 * 主题API
 */
import express from "express";
const theme = express.Router();
import { dbContext } from "../../database/initialize.js";

// 获取全部主题
theme.get("/theme", (req, res) => {
    try {
        const stmr = dbContext.db.prepare(`SELECT * FROM fps_theme`);
        const themes = [];
        while (stmr.step()) {
            themes.push(stmr.getAsObject());
        };
        stmr.free();
        res.status(200).json({success: true, data: themes, message: "全部主题获取成功"})
    } catch (error) {
        console.error("/api/theme请求时发生错误：", error);
        res.status(500).json({success: false, data: error, message: "/api/theme 请求时发生错误"})
    }
});

// 获取当前使用主题
theme.get("/theme/active", (req, res) => {
    try {
        const stmt = dbContext.db.prepare(`SELECT * FROM fps_theme WHERE THEME_ISOK = 1 LIMIT 1`);
        const result = stmt.step() ? stmt.getAsObject() : null;
        stmt.free();
        if(!result) {
            return res.status(200).json({success: false, data: result, message: "当前没有启用的主题"});
        };
        res.status(200).json({success: true, data: result, message: "获取启用主题成功"})
    } catch (error) {
        console.error("/theme/active请求时发生错误", error);
        res.status(500).json({success: false, data: error, message: "/theme/active 请求时发生错误"})
    }
})

// 根据主键获取主题
theme.get("/theme/primary/key", (req, res) => {
    try {
        const { ID } = req.query;
        if(!ID) {
            return res.status(400).json({success: false, data: null, message: "请传入需要获取主题的ID"});
        };
        const stmr = dbContext.db.prepare(`SELECT * FROM fps_theme WHERE THEME_ID = ?`);
        stmr.bind([ID]);
        const result = stmr.step() ? stmr.getAsObject() : null;
        stmr.free();
        if(!result) {
            return res.status(200).json({success: false, data: result, message: "未查询指定主题，请核对后再进行查询"});
        };
        res.status(200).json({success: true, data: result, message: "获取主题成功"});
    } catch (error) {
        console.error("/theme/primary/key 请求时发生错误：", error);
        res.status(500).json({success: false, data: error, message: "/theme/primary/key 请求时发生错误"})
    }
});

// 设置主题
theme.put("/theme/set", (req, res) => {
    dbContext.db.exec("BEGIN TRANSACTION");
    try {
        const { ID } = req.body;
        if(!ID) {
            dbContext.db.exec("ROLLBACK");
            return res.status(400).json({success: false, data: null, message: "请传入需要设置主题的ID"});
        };
        const checkStmt = dbContext.db.prepare(`SELECT THEME_ID FROM fps_theme WHERE THEME_ID = ?`);
        checkStmt.bind([ID]);
        const themeExists = checkStmt.step();
        themeExists.free();
        if(!themeExists){
            dbContext.db.exec("ROLLBACK");
            return res.status(200).json({success: false, data: null, message: "设置的主题不存在"})
        };
        const disableAllStmt = dbContext.db.prepare(`UPDATE fps_theme SET THEME_ISOK = 0, UPDATED_AT = CURRENT_TIMESTAMP`);
        disableAllStmt.step();
        disableAllStmt.free();
        const enableStmt = dbContext.db.prepare(`UPDATE fps_theme SET THEME_ISOK = 1, UPDATED_AT = CURRENT_TIMESTAMP WHERE THEME_ID = ?`);
        enableStmt.bind([ID]);
        const result = enableStmt.step();
        enableStmt.free();
        if(!result) {
            dbContext.db.exec("ROLLBACK");
            return res.status(200).json({success: false, data: null, message: "主题设置失败"});
        };
        dbContext.db.exec("COMMIT");
        res.status(200).json({success: true, data: ID, message: "设置主题成功"});
    } catch (error) {
        dbContext.db.exec("ROLLBACK");
        console.error("/theme/set 请求时发生错误：", error);
        res.status(500).json({success: false, data: error, message: "/theme/set 请求时发生错误"})
    }
});


export default theme;